<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="维修类型" prop="repairStyle">
        <el-select
            v-model="queryParams.repairStyle"
            placeholder="请选择维修类型"
            style="width: 160px"
            clearable
            @keyup.enter="handleQuery"
        >
          <el-option
              v-for="item in repairStyleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!-- 新增：投诉状态下拉框 -->
      <el-form-item label="维修状态" prop="repairStatus">
        <el-select
            v-model="queryParams.repairStatus"
            placeholder="请选择维修状态"
            style="width: 160px"
            clearable
            @keyup.enter="handleQuery"
        >
          <el-option
              v-for="item in repairStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['repairWXBe:repairYYYWXBe:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairYYYWXBeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="手机" align="center" prop="userPhone" />
      <el-table-column label="维修类型" align="center" prop="repairStyle" />
      <el-table-column label="维修描述" align="center" prop="repairInfo" />
      <el-table-column label="照片描述" align="center" prop="repiirPhoto" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.repiirPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="维修状态" align="center" prop="repairStatus">
        <template #default="scope">
          <el-tag :type="getRepairStatusTagType(scope.row.repairStatus)">
            {{ getRepairStatusText(scope.row.repairStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['repairWXBe:repairYYYWXBe:edit']">详情</el-button>
          <el-button link type="danger" icon="Delete" v-if="scope.row.repairStatus === 1" @click="handleDelete(scope.row)" v-hasPermi="['repairWXBe:repairYYYWXBe:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="repairYYYWXBeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" disabled placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机" prop="userPhone">
          <el-input v-model="form.userPhone" disabled placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="维修类型" prop="repairStyle">
          <el-select v-model="form.repairStyle" disabled placeholder="请选择维修类型" clearable>
            <el-option
                v-for="item in repairStyleOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修描述" prop="repairInfo">
          <el-input v-model="form.repairInfo" disabled type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="照片描述" prop="repiirPhoto">
          <image-upload disabled v-model="form.repiirPhoto"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RepairYYYWXBe">
import { listRepairYYYWXBe, getRepairYYYWXBe, delRepairYYYWXBe, addRepairYYYWXBe, updateRepairYYYWXBe } from "@/api/repairWXBe/repairYYYWXBe"

const { proxy } = getCurrentInstance()

const repairYYYWXBeList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    userPhone: null,
    repairStyle: null,
    repairInfo: null,
    repiirPhoto: null,
    repairStatus: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询维修记录列表 */
function getList() {
  loading.value = true
  listRepairYYYWXBe(queryParams.value).then(response => {
    repairYYYWXBeList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userName: null,
    userPhone: null,
    repairStyle: null,
    repairInfo: null,
    repiirPhoto: null,
    repairStatus: null,
    createTime: null
  }
  proxy.resetForm("repairYYYWXBeRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加维修记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRepairYYYWXBe(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "维修记录详情"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["repairYYYWXBeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRepairYYYWXBe(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRepairYYYWXBe(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除维修记录编号为"' + _ids + '"的数据项？').then(function() {
    return delRepairYYYWXBe(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('repairWXBe/repairYYYWXBe/export', {
    ...queryParams.value
  }, `repairYYYWXBe_${new Date().getTime()}.xlsx`)
}


const repairStyleOptions = ref([
  { label: "水电维修（水龙头/水管/电路）", value: "水电维修" },
  { label: "家电维修（空调/冰箱/洗衣机）", value: "家电维修" },
  { label: "墙面地面（墙皮脱落/地板损坏）", value: "墙面地面维修" },
  { label: "门窗维修（门锁/窗户/门把手）", value: "门窗维修" },
  { label: "家具维修（桌椅/橱柜/衣柜）", value: "家具维修" },
  { label: "公共设施（楼道灯/电梯/门禁）", value: "公共设施维修" },
  { label: "其他维修", value: "其他维修" }
]);

const getRepairStatusText = (state) => {
  switch (state) {
    case 0: return '待处理';
    case 1: return '已处理';
    default: return '未知状态';
  }
};

const getRepairStatusTagType = (state) => {
  switch (state) {
    case 1: return 'success';
    case 0: return 'danger';
    default: return 'warning';
  }
};

const repairStatusOptions = ref([
  { label: "待处理", value: 0 },  // value需与数据库存储的状态值一致（数字类型）
  { label: "已处理", value: 1 }
]);

getList()
</script>
